﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Microsoft.Security.Application;
using Pai.LiveStation.Data;
using System.Configuration;

namespace Pai.LiveStation.Web.Controllers
{
    public class RadioShowsController : Controller
    {
        //
        // GET: /RadioShows/

        public ViewResult Index()
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                 return View(dc.RadioShows.ToList<RadioShow>());
            }
        }

        public ViewResult Details(RadioShow show)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                ViewData["IsProducer"] = false;

                if(Request.IsAuthenticated)
                    ViewData["IsProducer"] = show.RadioShowsMembers.Where(x=>x.UserID.Equals((Guid)Membership.GetUser().ProviderUserKey)).FirstOrDefault().IsProducer;

           
                List<Tag> tags = show.RadioShowsTags.Select(x=>x.Tag).ToList<Tag>();

                ViewData["Tags"] = CommonHelper.RankTags(tags);
                return View(show);
            }
           
        }
        //[Authorize(Roles = "Managers")]
        public ViewResult Create()
        {
            return View();
        }

       // [Authorize(Roles = "Managers")]
        [AcceptVerbs(HttpVerbs.Post)]
        public RedirectToRouteResult Create(RadioShow show)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                show.Name = AntiXss.HtmlEncode(show.Name);
                show.Description = AntiXss.HtmlEncode(show.Description);
                if (ModelState.IsValid)
                {
                    dc.RadioShows.InsertOnSubmit(show);
                    dc.SubmitChanges();
                    
                }
                return RedirectToAction("Index", "RadioShows");
            }
        }

       // [Authorize(Roles = "Managers")]
        public ViewResult Edit(int id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                RadioShow show = dc.RadioShows.Where(x => x.ID == id).FirstOrDefault();
               
                if (show == null)
                    throw new ArgumentException("Invalid radio show.");

                return View(show);
            }
        }

      //  [Authorize(Roles = "Managers")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(RadioShow show)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {
                RadioShow updateShow = dc.RadioShows.Where(x => x.ID == show.ID).FirstOrDefault();

                if (updateShow == null)
                    throw new ArgumentException("Invalid radio show");
                
                updateShow.Name = AntiXss.HtmlEncode(show.Name);
                updateShow.Description = AntiXss.HtmlEncode(show.Description);

                //showTags.Split(';').ToList<string>().ForEach(x =>
                //{
                //    string tagName = AntiXss.HtmlEncode(x);
                //    Tag tag = dc.Tags.Where(y=>y.ID.Equals(tagName)).FirstOrDefault();
                //    if(tag == null)
                //    {
                //        tag.ID = tagName;
                //        dc.Tags.InsertOnSubmit(tag);
                //    }
                //    show.RadioShowsTags.Add(new RadioShowsTag() { Tag = tag, RadioShow = show});
                //});

                if (ModelState.IsValid)
                {
                    dc.SubmitChanges();
                }
                return View(show);
            }
        }
      //  [Authorize(Roles = "Managers")]
        public RedirectToRouteResult Delete(int id)
        {
            using (RadioClassesDataContext dc = new RadioClassesDataContext())
            {

                RadioShow show = dc.RadioShows.Where(x => x.ID == id).FirstOrDefault();
                if (show != null)
                {
                    dc.RadioShows.DeleteOnSubmit(show);
                    dc.SubmitChanges();
                }
                else
                    throw new ArgumentException("Could not find radio show.");
               
                return RedirectToAction("Index", "RadioShows");
            }
        }
    }
}
